/**
 * 用户workerInfo.js
 * 版本号： v1.0.0
 */
$(function(){
    //查询所属项目绑定
    bindCombo_project("prId_");

    var lastChecked = null;
    $("#workerInfo_dataList").datagrid({
        fitColumns: true,
        fit:true,
        method: "POST",
        rownumbers: true,
        singleSelect:true,
        pagination: true,
        border: false,
        pageSize:10,
        pageList:[10,30,50,100],
        url: CONTEXT_PATH + '/workerInfo/workerInfoList.do',
        idField: 'cardNo',
        frozenColumns:[[
            {field:'cardNo',checkbox:true}
        ]],
        columns: [
            [
                {field: 'projectId', title: '项目id', width: 180, fixed: true,hidden:true},
                {field: 'projectName', title: '所属项目', width: 180, fixed: true},
                {field: 'name', title: '姓名', width: 80, fixed: true},
                {field: 'idCard', title: '身份证号', width: 140, fixed: true},
                {field: 'sex', title: '性别', width: 50, fixed: true},
                {field: 'age', title: '年龄', width: 50, fixed: true},
                {field: 'birthday', title: '出生年月', width: 100, fixed: true},
                {field: 'work', title: '工种', width: 80, fixed: true,formatter: function (val) {
                        if(val) {
                            return val.name;
                        }
                }},
                {field: 'group', title: '班组', width: 100,fixed: true,formatter: function (val) {
                        if(val) {
                            return val.name;
                        }
                }},
                {field: 'deleted', title: '状态', width: 80,
                    formatter:function(val,row,index){
                        if(val == 0){
                            return '<span style="color: green; font-weight: bold">在职</span>';
                        }
                        if(val == 1){
                            return '<span style="color: red; font-weight: bold">离职</span>';
                        }
                    }
                }
            ]
        ],
        onCheck:function(rowIndex, rowData){
            var row = $("#workerInfo_dataList").datagrid("getChecked");
            if(row && rowIndex == lastChecked){
                $('#workerInfo_dataList').datagrid("unselectAll");//取消选中当前所有行
                lastChecked = null;
            }else{
                lastChecked = rowIndex;
            }
        },
        onLoadSuccess:function(data){
            if(data.rows.length > 0){
                lastChecked = null;
                $("#workerInfo_dataList").datagrid("selectRow", 0);
            }
        }
    });

    //定义可操作按钮
    $("#workerInfo_menu").datagrid({
        border: false,
        fit: true,
        toolbar: createToolbar()
    })

});

/**
 * 创建toolbar按钮组
 * @returns {Array}
 */
function createToolbar() {
    var toolbar = [];
    var falg = false;
    for (var j = 0; j < btnOptions.length; j++) {
        if (false != btnOptions[j].value) {
            falg = true;
            break;
        }
    }
    if (!falg) {
        //不存在按钮
        toolbar.push({});
    } else {
        //存在按钮
        for (var i = 0; i < btnOptions.length; i++) {
            if (false != btnOptions[i].value) {
                //查询按钮
                if ("searchBtn" == btnOptions[i].key) {
                    toolbar.push({
                        id:btnOptions[i].value.meId,
                        text:btnOptions[i].value.name,
                        iconCls:btnOptions[i].value.iconCls,
                        handler:function(){
                            workerInfo_findList();
                        }
                    });
                }
                //刷新按钮
                if ("refreshBtn" == btnOptions[i].key) {
                    toolbar.push({
                        id:btnOptions[i].value.meId,
                        text:btnOptions[i].value.name,
                        iconCls:btnOptions[i].value.iconCls,
                        handler:function(){
                            workerInfo_refresh();
                        }
                    });
                }
            }
        }
    }
    return toolbar;
}

/**
 * 刷新用户
 */
function workerInfo_refresh(){
    $("#workerInfo_dataList").datagrid("reload",{});
    $("#workerInfo_dataList").datagrid("clearSelections");
}

//查询用户
function workerInfo_findList(){
    $("#workerInfoQuery_dialog").dialog({
        title: '人员查询',
        width: 430,
        modal: true,
        closed: true,
        height: 220,
        iconCls:"icon-custom-search",
        buttons:[{
            text:"查询",
            iconCls:"icon-role-state-ok",
            handler:function(){
                //刷新dg页码
                refreshPageNumber("workerInfo_dataList");
                $("#workerInfo_dataList").datagrid("reload",{projectId:$("#prId_").combobox("getValue"),
                    userName:$("#userName_").textbox("getValue"),
                    workName:$("#workName_").textbox("getValue")});
                //清楚选中项
                $("#workerInfo_dataList").datagrid("clearSelections");
                $('#workerInfoQuery_dialog').dialog('close');
            }
        },{
            text:"取消",
            iconCls:"icon-role-state-no",
            handler:function(){
                $('#workerInfoQuery_dialog').dialog('close');
            }
        }]
    });
    $("#id_").val("");
    $("#workerInfoQuery_dialog").dialog("move",{left:450,top:50});
    $("#workerInfoQuery_dialog").dialog('open');
}



/**
 * 所属项目bind事件
 * @param projectControl
 */
function bindCombo_project(projectControl) {
    $.ajax({
        url: CONTEXT_PATH + "/project/getProjectListForCombo.do",
        type: "get",
        success: function (data) {
            var obj = JSON.parse(data);
            var projectCombo = [{'text': '--请选择--', 'value': ''}];
            for (var i = 0; i < obj.length; i++) {
                projectCombo.push({"text": obj[i].prName, "value": obj[i].prId});
            }
            $("#" + projectControl).combobox("loadData", projectCombo);
        }
    })
}

